package com.onccc.social.step

import android.view.accessibility.AccessibilityNodeInfo
import com.blankj.utilcode.util.AppUtils
import com.onccc.social.Assists
import com.onccc.social.Assists.click
import com.onccc.social.Assists.getBoundsInScreen
import com.onccc.social.Assists.getChildren
import com.onccc.social.Assists.paste
import com.onccc.social.MainActivity
import com.onccc.social.MainActivity.Companion.getInputTime
import com.onccc.social.MainActivity.Companion.getOneTime
import com.onccc.social.MainActivity.Companion.getThreeTime
import com.onccc.social.MainActivity.Companion.getTwoTime
import com.onccc.social.OverManager
import com.onccc.social.model.CopyWriteModel
import com.onccc.social.stepper.Step
import com.onccc.social.stepper.StepCollector
import com.onccc.social.stepper.StepImpl
import com.onccc.social.stepper.StepManager
import kotlinx.coroutines.delay


/**
 * @author GongChengHao
 * @date 2024/12/4
 * Description :
 */
class YouTubeCreateSocial : StepImpl() {

    var mPackageName = "com.google.android.youtube"

    private var writeModel = CopyWriteModel()

    override fun onImpl(collector: StepCollector) {
        collector.next(StepTag.STEP_1) {
            OverManager.log("启动youtube")

            AppUtils.launchApp(mPackageName)
            runIO { delay(getTwoTime()) }

            Assists.findByText("Accept").forEach() {
                runIO { delay(1000) }
                it.click()
            }
            return@next Step.get(StepTag.STEP_2)
        }.next(StepTag.STEP_2) {
            Assists.findByText("Create").forEach {
                runIO { delay(getOneTime()) }
                val screen = it.getBoundsInScreen()
                if (screen.top > 1750) {
                    OverManager.log("点击创建")
                    Assists.gestureViewClick(it)
                    return@next Step.get(StepTag.STEP_3)
                }
            }

            if (Assists.getPackageName() == mPackageName) {
                runIO { delay(getOneTime()) }
                Assists.back()
                return@next Step.get(StepTag.STEP_2)
            }
            return@next Step.get(StepTag.STEP_1)
        }.next(StepTag.STEP_3) {
            runIO { delay(getOneTime()) }
            Assists.findById("com.google.android.youtube:id/reel_camera_gallery_button_delegate").forEach {
                runIO { delay(getTwoTime()) }
                OverManager.log("进入相册添加")
                Assists.gestureViewClick(it)
                return@next Step.get(StepTag.STEP_4)
            }

            return@next Step.get(StepTag.STEP_2)
        }.next(StepTag.STEP_4) {
            runIO { delay(getOneTime()) }
            Assists.findByTags("android.widget.GridView").forEach {
                it.getChildren().forEach { item ->
                    if (item.getChild(0).childCount > 2) {
                        runIO { delay(getOneTime()) }
                        Assists.gestureViewClick(item)
                        return@next Step.get(StepTag.STEP_5)
                    }
                }

            }
            return@next Step.get(StepTag.STEP_100)
        }.next(StepTag.STEP_5) {
            runIO { delay(getOneTime()) }
            Assists.findById("com.google.android.youtube:id/multi_select_next_button").forEach {
                runIO { delay(getTwoTime()) }
                OverManager.log("选择视频下一步")
                Assists.gestureViewClick(it)
                return@next Step.get(StepTag.STEP_6)
            }

            return@next Step.get(StepTag.STEP_2)
        }.next(StepTag.STEP_6) {
            runIO { delay(getOneTime()) }
            Assists.findByText("Done").forEach {
                runIO { delay(getTwoTime()) }
                OverManager.log("确定视频")
                Assists.gestureViewClick(it)
                return@next Step.get(StepTag.STEP_7)
            }
            return@next Step.get(StepTag.STEP_2)
        }.next(StepTag.STEP_7) {
            runIO { delay(getThreeTime()) }
            Assists.findById("com.google.android.youtube:id/shorts_camera_next_button_delegate").forEach {
                runIO { delay(getTwoTime()) }
                OverManager.log("视频选择完成")
                Assists.gestureViewClick(it)
                return@next Step.get(StepTag.STEP_8)
            }
            return@next Step.repeat
        }.next(StepTag.STEP_8) {
            runIO { delay(getOneTime()) }
            Assists.findById("com.google.android.youtube:id/shorts_post_bottom_button").forEach {
                runIO { delay(getTwoTime()) }
                OverManager.log("确定发送")
                Assists.gestureViewClick(it)
                return@next Step.get(StepTag.STEP_9)
            }
            return@next Step.get(StepTag.STEP_100)
        }.next(StepTag.STEP_9) {
            runIO { delay(getTwoTime()) }
            MainActivity.getPostContent("youtube", { model ->
                writeModel = model
                StepManager.execute(YouTubeCreateSocial::class.java, StepTag.STEP_10, begin = true)
            })
            {
                StepManager.execute(YouTubeCreateSocial::class.java, StepTag.STEP_10, begin = true)

            }
            return@next Step.none
        }.next(StepTag.STEP_10) {
            runIO { delay(getOneTime()) }
            OverManager.log("点击输入框")
            Assists.findByTags("android.widget.EditText").forEach {
//               val editNode =  it.getChild(0).getChild(0).getChild(1).getChild(0).getChild(0)
                runIO { delay(getTwoTime()) }
                Assists.gestureViewClick(it)

                return@next Step.get(StepTag.STEP_11)
            }
            return@next Step.get(StepTag.STEP_100)
        }.next(StepTag.STEP_11) {
            runIO { delay(getOneTime()) }
            OverManager.log("输入发送内容")
            Assists.findByTags("android.widget.EditText").forEach {
//                val editNode =  it.getChild(0).getChild(0).getChild(1).getChild(0).getChild(0)
                runIO { delay(getTwoTime()) }
                Assists.gestureViewClick(it)

                it.performAction(AccessibilityNodeInfo.ACTION_FOCUS)
                for (s in writeModel.copywriting) {
                    it.paste(s.toString())
                    runIO { delay(getInputTime()) }
                }
                if (writeModel.hashtags.size == 0) {
                    return@next Step.get(StepTag.STEP_12)
                }
                var hashtag = ""
                for (s in writeModel.hashtags) {
                    hashtag += "#$s "
                }
                for (hs in hashtag) {
                    it.paste(hs.toString())
                    runIO { delay(getInputTime()) }
                }

                runIO { delay(getOneTime()) }
                Assists.back()
                return@next Step.get(StepTag.STEP_12)
            }
            return@next Step.get(StepTag.STEP_100)
        }.next(StepTag.STEP_12) {
            runIO { delay(getTwoTime()) }
            Assists.findById("com.google.android.youtube:id/upload_bottom_button").firstOrNull() {
                OverManager.log("确定发送")
                runIO { delay(getThreeTime()) }
                Assists.gestureViewClick(it)
                return@next Step.get(StepTag.STEP_100)
            }
            return@next Step.get(StepTag.STEP_12)
        }.next(StepTag.STEP_97) {
            runIO { delay(getOneTime()) }
            Assists.back()
            return@next Step.get(StepTag.STEP_98)
        }.next(StepTag.STEP_100) {
            OverManager.log("结束")
            return@next Step.none
        }
    }
}